
package my.algorithms.mcorrea.topcoder;

/**
 * Julius Caesar used a system of cryptography, now known as Caesar Cipher, 
 * which shifted each letter 2 places further through the alphabet 
 * (e.g. 'A' shifts to 'C', 'R' shifts to 'T', etc.). At the end of the alphabet
 * we wrap around, that is 'Y' shifts to 'A'.

We can, of course, try shifting by any number. Given an encoded text 
* and a number of places to shift, decode it.

For example, "TOPCODER" shifted by 2 places will be encoded as "VQREQFGT". 
* In other words, if given (quotes for clarity) "VQREQFGT" and 2 as input, 
* you will return "TOPCODER". See example 0 below.
 * @author mcorrea
 */
public class CCipher {
	
	public static void main(String args[]){
		String str = "VQREQFGT";
		System.out.println( decode(str, 2)  );
		System.out.println( decode("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 10)  );
		
	}
	
	public static String decode(String cipherText, int shift){
		String decode = "";
		for(char c: cipherText.toCharArray()){
			int n = ((int)c - shift);
			if(n<'A'){
				n = (int)c + 26 - shift; //shift = (c - 64 + 90-n)
			}
			decode += ""+ (char)n;
		}
		
		return decode;
	}
	
}
